lobsterh5.main({
    data: {
        regtype:1,
        recordinfo: {},
        patientlist: [],
        selectpatient: {},
    },
    pageload() {

        this.data.regtype = lobsterh5.GetUrlParam("regtype");

        this.initdata();
        this.initevent();

        this.inittemp("tplrecord", "record", this.data.recordinfo);
    },
    initevent() {
        var self = this;
        $(document).on("click", "#patiengdiv", function () {
            $("#half").popup();

            self.inittemp("tplcard", "sidebaritem", { list: self.data.patientlist });
        })

        $(document).on("click", ".carditem", function (obj) {
            var card = obj.currentTarget.dataset.info;
            var info = JSON.parse(card);
            self.data.selectpatient = info;
            //测试就诊人 （身份证：320***********9739）
            $("#selectedPatient").html(info.Name + "  (" + info.IDCard + " )");
            $.closePopup()
        });
        $(document).on("click", "#register", function () {
            // console.log(self.data.selectpatient);
            // console.log(self.data.recordinfo);
            self.register();
        })
    },
    initdata() {
        var self = this;

        self.data.recordinfo = JSON.parse(localStorage.getItem("registerrecord"));

        lobsterh5.GET("/admin/v1/CPatient/GetUserPatient").then(res => {
            self.data.patientlist = res.patdata;
        }).catch(res => {
            $.toast(res.msg, 'text');
        });

    },
    inittemp(tempid, _id, data) {
        var html = template(tempid, data);
        $("#" + _id).html(html);
    },
    register() {
        var self = this;
        console.log(self.data.selectpatient);
        console.log(self.data.recordinfo);
        if (!self.data.selectpatient || !self.data.selectpatient.PatientId) {
            $.toast("请选择就诊人", 'text');
            return;
        }
        var patient = self.data.selectpatient;
        var recrodinfo = self.data.recordinfo;
        var data = {};
        data.Id = 0;
        data.RegType = self.data.regtype;
        data.MPUserID = patient.MPUserId;
        data.PatName = patient.Name;
        data.PatientId = patient.PatientId;
        data.DeptId = recrodinfo.DeptId;
        data.DeptName = recrodinfo.DeptName;
        data.HisDeptId = recrodinfo.DeptId;
        data.EmpId = recrodinfo.DoctId;
        data.EmpName = recrodinfo.DocName;
        data.Regid = recrodinfo.RegId;
        data.Timeid = recrodinfo.Timeid;
        data.PayAmt = recrodinfo.RegFee;
        data.RegDate = recrodinfo.RegDate;
        data.RegState = 1;
        data.RegRange = recrodinfo.StartTime + '-' + recrodinfo.EndTime;
        data.RegSchedType = recrodinfo.TimeType;
        data.DeleteFlag = 0;
        data.Sex = patient.Sex;
        data.Birthday = patient.Birthday;
        data.Phone = patient.Phone;

        lobsterh5.GET("/admin/v1/CPatient/GetCardTypeListByPId", { patientId: patient.PatientId }).then(res => {
            var cardList = res.cardList;//已绑卡
            var cardTypeList = res.cardTypeList;//卡类型字典
            //先找诊疗卡，未绑定诊疗卡则找身份证
            var arr = cardList.filter(x => x.Code == '1');
            if (arr.length > 0) {
                data.CardType = 1;//诊疗卡
                data.CardNum = arr[0].CardNo;
            } else {
                data.CardType = 2;//身份证
                data.CardNum = cardList.filter(x => x.Code == '2')[0].CardNo;
            }

            //保存预约挂号记录
            lobsterh5.POSTAPI('tsregister.register', data).then(retdata => {

                $.toast('进入收银台');

                setTimeout(function () {
                    location.href = retdata.url;
                }, 1000)

            }).catch(res => {
                $.toast(res.msg, 'forbidden');
            })
        }).catch(res => {
            $.toast(res.msg, 'forbidden');
        });

    },
});
